myApp
    .controller('bookMenuCtrl', function ($cookieStore,$scope,$http,CONFIG) {

        /*根据user cookie 初始化加载菜单*/
        $scope.isAdmin = $cookieStore.get("userRole") == "admin";
        $scope.userName = $cookieStore.get("userName");
        /*初始化，加载今日菜单*/
        var url = CONFIG.mainConfig.url+"/m02/u02/findAllDayMenuDetail";
        var date = new Date();
        var formatDate = date.toLocaleDateString();
        $http({
            method:'post',
            url:url,
            params:{
                today:formatDate
            }
        })
            .success(function (result,status) {

                if (result.rtnCode == 0000000) {
                    $scope.empTodayMenus=result.bizData;
                }
                else {
                    alert(result.msg);
                }

            })
            .error(function () {
                alert("error");
            });

        /*初始化加载员工已点的餐*/
        $http({
            method:'post',
            url:url,
            params:{
                today:formatDate,
                emp_no:"temp"
            }
        })
            .success(function (result,status) {

                if (result.rtnCode == 0000000) {
                    $scope.orderedMenus=result.bizData;
                }
                else {
                    alert(result.msg);
                }

            })
            .error(function () {
                alert("error");
            });

        /*员工点餐*/
        $scope.empBookMenu = function(menu_id) {

            var url = CONFIG.mainConfig.url+"/m02/u03/insertDayEmpMenu1";
            var date = new Date();
            var formatDate = date.toLocaleDateString();
            var Hour = date.getHours();
            var minute = date.getMinutes();
            if (Hour > CONFIG.mainConfig.Hour) {
                alert('已经超过点餐时间！');
            }else if(Hour==CONFIG.mainConfig.Hour&&minute>CONFIG.mainConfig.minute){
                alert('已经超过点餐时间！');
            } else {
                $http({
                    method: 'post',
                    url: url,
                    params: {
                        menu_id: menu_id,
                        today: formatDate
                    }
                })
                    .success(function (result, status) {

                        if (result.rtnCode == 0000000) {
                            /*把已点的菜同步显示*/
                            var url = CONFIG.mainConfig.url+"/m02/u02/findAllDayMenuDetail";
                            var date = new Date();
                            var formatDate = date.toLocaleDateString();
                            $http({
                                method: 'post',
                                url: url,
                                params: {
                                    today: formatDate,
                                    emp_no: "temp"
                                }
                            })
                                .success(function (result, status) {
                                    /*alert(result.rtnCode+","+status);*/

                                    if (result.rtnCode == 0000000) {
                                        $scope.orderedMenus = result.bizData;
                                    }
                                    else {
                                        alert(result.msg);
                                    }

                                })
                                .error(function () {
                                    alert("error");
                                });
                        }
                        else {
                            alert(result.msg);
                        }

                    })
                    .error(function () {
                        alert("error");
                    });
            }
            ;

            /*员工取消点餐*/
            $scope.empUnBookMenu = function (index) {
                $scope.orderedMenus.splice(index, 1);
                var url = CONFIG.mainConfig.url+"/m02/u03/deleteDayEmpMenu1";
                var date = new Date();
                var formatDate = date.toLocaleDateString();
                $http({
                    method: 'post',
                    url: url,
                    params: {
                        date: formatDate
                    }
                })
                    .success(function (result, status) {

                        if (result.rtnCode == 0000000) {
                            //TODO:
                        }
                        else {
                            alert(result.msg);
                        }

                    })
                    .error(function () {
                        alert("error");
                    });
            }
        }
    });